#include "imagewindow.h"
#include "imageview.h"

#include <qfiledialog.h>
#include <qdebug.h>
#include "pubdef.h"

ImageWindow::ImageWindow(QWidget *parent, QString file)
    : QMainWindow(parent)
{
    m_box = new ImageView();
    QScrollArea *scrollArea = new QScrollArea();
    scrollArea->setMinimumSize(800, 600);
    scrollArea->setWidgetResizable(true);
    scrollArea->setWidget(m_box);

    QHBoxLayout *layout_view = new QHBoxLayout();
    layout_view->addWidget(scrollArea);

    m_prevBtn = new QPushButton();
    //    m_prevBtn->setIcon(QIcon(":/images/prev.png"));
    m_prevBtn->setText("Previous");
    m_prevBtn->setFlat(true);
    m_prevBtn->setIconSize(QSize(50,50));

    m_openBtn = new QPushButton();
    //    m_openBtn->setIcon(QIcon(":/images/openimg.png"));
    m_openBtn->setText("Open File");
    m_openBtn->setFlat(true);
    m_openBtn->setIconSize(QSize(70,70));

    m_nextBtn = new QPushButton();
    //        m_nextBtn->setIcon(QIcon(":/images/next.png"));
    m_nextBtn->setText("Next");
    m_nextBtn->setFlat(true);
    m_nextBtn->setIconSize(QSize(50,50));

    m_nextBtn->setEnabled(false);
    m_prevBtn->setEnabled(false);

    QHBoxLayout *layout_button = new QHBoxLayout();
    layout_button->addWidget(m_prevBtn);
    layout_button->addWidget(m_openBtn);
    layout_button->addWidget(m_nextBtn);

    //主布局管理
    QVBoxLayout *layout_main = new QVBoxLayout();
    layout_main->setSpacing(5);
    layout_main->addLayout(layout_view);
    layout_main->addLayout(layout_button);
    layout_main->setStretch(0,10);
    layout_main->setStretch(1,1);
    layout_main->setContentsMargins(5,5,5,5);

    QWidget *widget = new QWidget();
    widget->setLayout(layout_main);
    this->setCentralWidget(widget);

    connect(m_nextBtn, SIGNAL(clicked(bool)), this, SLOT(show_next()));
    connect(m_prevBtn, SIGNAL(clicked(bool)), this, SLOT(show_prev()));
    connect(m_openBtn, SIGNAL(clicked(bool)), this, SLOT(open_files()));


    open_file(file);
}


void ImageWindow::show_prev()
{
    if (m_idx == 0)
    {
        m_prevBtn->setEnabled(false);
        qDebug() << m_idx;
        return;
    }
    qDebug() << m_idx;

    m_nextBtn->setEnabled(true);
    m_idx--;
    m_box->setImage(m_files[m_idx]);
}

void ImageWindow::show_next()
{
    if (m_idx == m_files.length() - 1)
    {
        m_nextBtn->setEnabled(false);
        qDebug() << m_idx;
        return;
    }
    qDebug() << m_idx;

    m_prevBtn->setEnabled(true);
    m_idx++;
    m_box->setImage(m_files[m_idx]);
}

void ImageWindow::open_file(QString file)
{

    if (file.isNull() || file.isEmpty())
    {
        return;
    }
    m_file = file;
    QString file_path = QFileInfo(m_file).absolutePath();
    qDebug() << file_path;

    QDir dir(file_path);
    _settings->setValue(SET_PICTURE_FOLDER, dir.absolutePath());
    dir.setFilter(QDir::Files | QDir::NoSymLinks);
    QFileInfoList list = dir.entryInfoList(QStringList() << "*.jpg"
                                           << "*.png"
                                           << "*.bmp"
                                           << "*.jpeg"
                                           << "*.tiff");
    m_files.clear();
    for (int i = 0; i < list.size(); i++)
    {
        QFileInfo fileInfo = list.at(i);
        qDebug() << fileInfo.absoluteFilePath();
        m_files.push_back(fileInfo.absoluteFilePath());

        if(m_file == m_files[i])
        {
            m_idx = i;
        }
    }

    qDebug() << m_files.size();
    m_box->setImage(m_files[m_idx]);// 显示图片
    m_prevBtn->setEnabled(true);
    m_nextBtn->setEnabled(true);
}

void ImageWindow::open_files()
{
    QString folder = _settings->value(SET_PICTURE_FOLDER, QDir::homePath()).toString();
    QString file = QFileDialog::getOpenFileName(this, "所有图片", folder, "Image Files (*.png *.jpg *.bmp *.jpeg *.tiff)");
    open_file(file);
}


ImageWindow::~ImageWindow()
{
    delete _settings;
}

